{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "08b4abf2-b021-452e-80ca-55c8f5ceb134",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests in /root/miniconda3/envs/python39/lib/python3.9/site-packages (2.29.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /root/miniconda3/envs/python39/lib/python3.9/site-packages (from requests) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /root/miniconda3/envs/python39/lib/python3.9/site-packages (from requests) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /root/miniconda3/envs/python39/lib/python3.9/site-packages (from requests) (1.26.15)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /root/miniconda3/envs/python39/lib/python3.9/site-packages (from requests) (2022.12.7)\n",
      "\u001B[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001B[0m\u001B[33m\n",
      "\u001B[0m"
     ]
    }
   ],
   "source": [
    "! pip install requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "751e0eba-61f6-4ea9-b34d-793d48be2f3d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"message\": \"success\",\n",
      "  \"result\": {\n",
      "    \"y\": [\n",
      "      0\n",
      "    ]\n",
      "  },\n",
      "  \"status\": 0\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "url = \"http://decisiontree-202310011.service/v1/models/decisiontree/predict\"\n",
    "\n",
    "payload = json.dumps({\n",
    "  \"age\": 30,\n",
    "  \"duration\": 487,\n",
    "  \"campaign\": 2,\n",
    "  \"pdays\": 999,\n",
    "  \"previous\": 0,\n",
    "  \"emp_var_rate\": -1.8,\n",
    "  \"cons_price_idx\": 92.893,\n",
    "  \"cons_conf_idx\": -46.2,\n",
    "  \"euribor3m\": 1.313,\n",
    "  \"nr_employed\": 5099.1\n",
    "})\n",
    "headers = {\n",
    "  'Authorization': 'admin',\n",
    "  'Content-Type': 'application/json'\n",
    "}\n",
    "\n",
    "response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "\n",
    "print(response.text)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f6656ce-329b-4803-bc87-9e66a4bf1da9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 弹性伸缩测试\n",
    "\n",
    "import requests\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "\n",
    "# 定义要启动的线程数（即客户端数）\n",
    "num_clients = 1\n",
    "request_num = 100\n",
    "try_num=0\n",
    "def load_test(url):\n",
    "    global try_num\n",
    "    # 发送GET请求\n",
    "    url = \"http://xx.xx.xx.xx/v1/models/decisiontree/metadata\"\n",
    "    headers = {\n",
    "      'Host': 'decisiontree-202310011.service.svc.cluster.local',\n",
    "      'Authorization': 'admin',\n",
    "      'Content-Type': 'application/json'\n",
    "    }\n",
    "    response = requests.request(\"GET\", url, headers=headers, data=payload)\n",
    "    # 打印响应状态码\n",
    "    print(try_num,'成功' if response.status_code==200 else '失败',json.loads(response.text).get(\"model_spec\",{}).get(\"version\",'未知'),flush=True)\n",
    "    \n",
    "    try_num+=1\n",
    "\n",
    "# 创建一个ThreadPoolExecutor实例\n",
    "with ThreadPoolExecutor(max_workers=num_clients) as executor:\n",
    "    # 使用executor提交load_test任务，参数为url\n",
    "    for _ in range(request_num):\n",
    "        executor.submit(load_test, url)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
